<?php namespace dmq;

/**
* A database class
*
* The database class is used to create a connection to 
* the database and deal with questions to and from the database
*
* @author dmq (master@dmq.se)
*/
class Database
{
	private $mysqli = null;
	private $transactionStatus = true;

	/**
	* function open
    * open db connection.
    */
	public function open()
	{
		$this->mysqli = new \mysqli( Settings::DB_HOST, Settings::DB_USER, Settings::DB_PASSWORD, Settings::DB_NAME );
		if ( $this->mysqli->connect_errno )
		{
			echo 'Could not connect to the database.';
			die();
		}
	}

	/**
	* function geterrors
    * get errors in the event.
    * @return mixed[] $error Array returns the errors in an array.
    */
	public function getErrors()
	{
		$errors['number'] = $this->mysqli->errno;
		$errors['msg'] = $this->mysqli->error;

		return $errors;
	}

	/**
	* function openNew
    * Open and return a new connection.
    * @return $connection returns a new database connection.
    */
	public function openNew()
	{
		$connection = new Database();
		$connection->open();

		return $connection;
	}
	
	/**
	* function close
    * close the database connection.
    */
	public function close()
	{
		$this->mysqli->close();
		$this->mysqli = null;
	}

	/**
	* function prepare
    * Open and return a new connection.
    * @param $sql String a sql-statment
    * @return return a statement object or boolean FALSE if an error occurred.
    */
	public function prepare( $sql )
	{
		return $this->mysqli->prepare( $sql );
	}


	/**
	* function lastInsertId
	* Retrieves the ID generated for an AUTO_INCREMENT column by the previous sql-query.
    * @return the value of the AUTO_INCREMENT field that was updated by the previous query or returns zero if there was no previous query on the connection or if the query did not update an AUTO_INCREMENT value
    */
	public function lastInsertId()
	{
		return $this->mysqli->insert_id;
	}

	/**
	* function startTransaction
    * Turns off auto-commit mode on queries for the database connection and start a tranasction by turning it to on (true).
    */
	public function startTransaction()
	{
		$this->mysqli->autocommit( false );
		$this->transactionStatus = true;
	}

	/**
	* function execute
    * Executes a query that has been previously prepared using the prepare function, if the statement has passed the transactionStatus is true.
    * @return boolean true || false transactionStatus depending on success och failure.
    */
	public function execute( $stmt )
	{
		if ( !$stmt->execute() )
		{
			$this->transactionStatus = false;
		}

		return $this->transactionStatus;
	}

	/**
	* function endTransaction
    * if things go wrong a rollback is sent otherwise a commit, autocommit is turned on again (true).
    * @return boolean true || false transactionStatus depending on success och failure.
    */
	public function endTransaction()
	{
		if ( !$this->transactionStatus )
		{
			$this->mysqli->rollback();
		}
		else
		{
			$this->mysqli->commit();
		}

		$this->mysqli->autocommit( true );

		return $this->transactionStatus;
	}

	/**
	* function rollback
    * Rollbacks the current transaction for the database.
    */
	public function rollback()
	{
		$this->mysqli->rollback();
	}
}
